package com.whcstar.springcloud.eurekaconsumerribbon.service;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

@Service
public class OperateService {

    private static Logger logger = LoggerFactory.getLogger(OperateService.class);

    @Autowired
    private RestTemplate restTemplate;

    @HystrixCommand(fallbackMethod = "fallback")
    public Integer add(Integer a, Integer b) {
        logger.info("----call ribbon----");
        String url = "http://eureka-service-provide/operate/add?a={a}&b={b}";
        Map<String, Integer> map = new HashMap<>();
        map.put("a", a);
        map.put("b", b);
        Integer result = restTemplate.getForObject(url, Integer.class, map);
        return result;
    }

    public Integer sub(Integer a, Integer b) {
        logger.info("----call ribbon----");
        String url = "http://eureka-service-provide/operate/sub?a={a}&b={b}";
        Map<String, Integer> map = new HashMap<>();
        map.put("a", a);
        map.put("b", b);
        Integer result = restTemplate.getForObject(url, Integer.class, map);
        return result;
    }


    public Integer fallback(Integer a, Integer b) {
        return 0;
    }
}
